package com.skyolder.milkbee.web.rest.my;

import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.domain.Leaguer;
import com.skyolder.milkbee.domain.User;
import com.skyolder.milkbee.dto.SimpleResult;
import com.skyolder.milkbee.security.SecurityUtils;
import com.skyolder.milkbee.service.LeaguerService;
import com.skyolder.milkbee.service.UserService;
import com.skyolder.milkbee.tool.FileUtil;
import com.skyolder.milkbee.web.rest.util.HeaderUtil;
import com.skyolder.milkbee.web.rest.util.PaginationUtil;
import com.skyolder.milkbee.web.util.WebUserUtil;

import io.swagger.annotations.ApiParam;

/**
 * REST controller for managing Leaguer.
 */
@RestController("myLeaguerResource")
@RequestMapping("/api/my")
public class LeaguerResource {

	private final Logger log = LoggerFactory.getLogger(LeaguerResource.class);

	@Inject
	private LeaguerService leaguerService;
	@Inject
	private UserService userService;
	
	@Inject 
	private WebUserUtil webUserUtil;

	@GetMapping("/focus")
	@Timed
	public ResponseEntity<List<Leaguer>> getMyFocus(@ApiParam Pageable pageable) throws URISyntaxException {
		log.debug("REST request to get a page of Leaguers");
		Optional<User> ouser = userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin());
		User user = ouser.get();
		Page<Leaguer> page = leaguerService.doQueryFocus(user.getId(), pageable);
		HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/myfocus");
		return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
	}

	@GetMapping("/fans")
	@Timed
	public ResponseEntity<List<Leaguer>> getMyFans(@ApiParam Pageable pageable) throws URISyntaxException {
		log.debug("REST request to get a page of Leaguers");
		Optional<User> ouser = userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin());
		User user = ouser.get();
		Page<Leaguer> page = leaguerService.doQueryFans(user.getId(), pageable);
		HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/myfans");
		return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
	}

	@GetMapping("/profile")
	@Timed
	public ResponseEntity<Leaguer> getProfile() {
		Optional<User> ouser = userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin());
		User user = ouser.get();
		Leaguer leaguer = leaguerService.findByUser(user);
		return Optional.ofNullable(leaguer).map(result -> new ResponseEntity<>(result, HttpStatus.OK))
				.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
	}

	@PutMapping("/profile")
	@Timed
	public ResponseEntity<Leaguer> updateProfile(@RequestBody Leaguer leaguer) throws URISyntaxException {
		log.debug("REST request to update Leaguer : {}", leaguer);
		if (leaguer.getId() == null) {
			return ResponseEntity.badRequest()
					.headers(HeaderUtil.createFailureAlert("leaguer", "idexists", "can not update nul id profile"))
					.body(null);
		}
		Leaguer dbProfile = leaguerService.findOne(leaguer.getId());
		dbProfile.setNikename(leaguer.getNikename());
		dbProfile.setBirth(leaguer.getBirth());
		dbProfile.setUnderwrite(leaguer.getUnderwrite());
		if (!dbProfile.isAuthentication()) {
			dbProfile.setIdentification(leaguer.getIdentification());
			dbProfile.setRealName(leaguer.getRealName());
			dbProfile.setIdPicture(leaguer.getIdPicture());
			dbProfile.setRealPicture(leaguer.getRealPicture());
		}
		dbProfile.setAvatar(leaguer.getAvatar());
		Leaguer result = leaguerService.save(dbProfile);
		return ResponseEntity.ok().headers(HeaderUtil.createEntityUpdateAlert("leaguer", result.getId().toString()))
				.body(result);
	}
	@PutMapping("/profile/useavatar/{index}")
	@Timed
	public ResponseEntity<SimpleResult> useStoreProfileAvatar(@PathVariable Integer index) throws URISyntaxException {
		SimpleResult simpleResult=new SimpleResult();
		User me= webUserUtil.getCurrentUser();
		String src=FileUtil.getWebRealPath("/content/images/store/avatar/"+index+".png");
		String dst=FileUtil.getWebRealPath("/content/images/upload/avatar/"+me.getId()+".png");
		FileUtil.cpFile(src,dst);
		return new ResponseEntity<>(simpleResult, HttpStatus.OK); 
	}
}
